#ifndef __UDP_SERVER_HPP__
#define __UDP_SERVER_HPP__
#include "Common.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <string>
#include <errno.h>
#include <memory>
#include "Log.hpp"
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include<functional>
#include "InetAddr.hpp"
using namespace LogModule;
using func_t =std::function<std::string(std::string)>;
const static int defaultsockfd = -1;
const static std::string defaultip = "127.0.0.1"; // 表示本地主机，用于代码测试，
const static uint16_t defaulteport = 8080;

class UdpServer
{

public:
    UdpServer(func_t func,const uint16_t port = defaulteport)
        : _ip(defaultip), _port(port), _sockfd(defaultsockfd), _isrunning(0), _addr(port),_func(func)
    {
    }
    void InitServer()
    {
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            LOG(LogLevel::FATAL) << "socket" << strerror(errno);
            Die(SOCKET_ERR);
        }
        LOG(LogLevel::INFO) << "socket success 值为" << _sockfd;

        // 填充网络信息
        //  struct sockaddr_in local;
        //  bzero(&local, sizeof local);
        //  local.sin_family = AF_INET;
        //  // 把端口号由主机系列转为网络序列
        //  local.sin_port = htons(_port);
        //  //local.sin_addr.s_addr = inet_addr(_ip.c_str());
        //  local.sin_addr.s_addr = INADDR_ANY;

        int n = ::bind(_sockfd, _addr.NetAddr(), _addr.NetAddrLen());
        if (n < 0)
        {
            LOG(LogLevel::FATAL) << "bind:" << strerror(errno);
            Die(BIND_ERR);
        }
        LOG(LogLevel::INFO) << "bind success";
    }
    void Start()
    {
        if (_isrunning)
            return;
        _isrunning = true;
        char buff[1024];
        sockaddr_in peer;
        socklen_t len = sizeof peer;
        while (true)
        {
            ssize_t n = recvfrom(_sockfd, buff, sizeof(buff) - 1, 0, CONV(&peer), &len);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "recvfrom fail" << strerror(errno);
                Die(3);
            }
            if (n > 0)
            {
                buff[n] = 0;
                //   uint16_t clientport=ntohs(peer.sin_port);
                //   std::string clientip=inet_ntoa(peer.sin_addr);
                //   LOG(LogLevel::DEBUG) <<clientip<<":"<<std::to_string(clientport)<<"#"<<buff;

                InetAddr cli(peer);
                std::string clientinfo = cli.Ip() + ":" + std::to_string(cli.Port()) + "#" + buff;
                 //显示客户端发了什么消息
                LOG(LogLevel::DEBUG) << clientinfo;
              
                std::string echo_string = "echo#";
                std::string result=_func(buff);
                echo_string += result;
                n = sendto(_sockfd, echo_string.c_str(), echo_string.size(), 0, CONV(&peer), sizeof peer);
                if (n < 0)
                {
                    LOG(LogLevel::FATAL) << "sendto fail" << strerror(errno);
                }
            }
        }
        _isrunning=false;
    }
    void End() {}
    ~UdpServer()
    {
        if (_sockfd > defaultsockfd)
        {
            close(_sockfd);
        }
    }

private:
    int _sockfd;
    uint16_t _port;
    std::string _ip;
    InetAddr _addr;
    bool _isrunning; // 服务器是否启动
    func_t _func;
};

#endif